// pages/exam_signin/exam_signin.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    exam_id: "",
    status: 0,
    dimen: [],
    right_num: 0,
    name: [],
    button_name: "提交签到",
    submit_button: true,
    flag: false,
    group: {
      _id: ""
    },
  },

  plus: function() {
    var dimen_temp = this.data.dimen
    dimen_temp.push("")
    var name_index = this.data.name
    name_index.push("")

    this.setData({
      dimen: dimen_temp,
      name: name_index
    })
  },

  substract: function(e) {
    var dimen_temp = this.data.dimen
    var index = e.currentTarget.dataset.index
    var name_index = this.data.name

    dimen_temp.splice(index, 1)
    name_index.splice(index, 1)

    this.setData({
      dimen: dimen_temp,
      name: name_index,
      right_num: dimen_temp.length
    })
  },

  dimen_input: function(e) {
    var that = this
    var index = e.currentTarget.dataset.index
    var id = e.detail.value
    console.log(e.detail.value)
    console.log(index)
    var dimen_temp = this.data.dimen
    dimen_temp[index] = id

    var name_index = this.data.name

    //查询学生名字
    wx.request({
      url: getApp().globalData.my_server + "/mark/get_name_by_id",
      data: {
        kind: "1",
        id: id
      },

      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function(res) {
        console.log(res)

        if (res.data.error_no == 2) {
          wx.showModal({
            title: '哎呀～',
            content: "该学号不存在" + res.data.error_msg,
            success: function(res) {
              if (res.confirm) {
                console.log('用户点击确定')
              } else if (res.cancel) {
                console.log('用户点击取消')
              }
            }
          })
          that.setData({
            submit_button: true
          })
        } else if (res.data.error_no == 1) {
          console.log("成功")
          name_index[index] = res.data.docs[0].name

          var right_num_temp = dimen_temp.length

          var submit_button_temp = 0

          if (that.data.status){
            submit_button_temp = 1
          }

          that.setData({
            dimen: dimen_temp,
            name: name_index,
            flag: true,
            submit_button: submit_button_temp,
            right_num: right_num_temp
          })
        }
      },

    })

  },

  formSubmit: function(e) { 
    var that = this
    console.log(e.currentTarget)
    console.log(e.detail.value)
    var list = e.detail.value

    if (list.group_name == "") {
      wx.showModal({
        title: '提示～',
        content: '组名不能为空',
        success: function(res) {
          if (res.confirm) {
            console.log('用户点击确定')
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    } else if (list.group_name != "") {
      if (that.data.right_num != that.data.dimen.length) {
        wx.showModal({
          title: '提示～',
          content: '有的学号不存在',
          success: function(res) {
            if (res.confirm) {
              console.log('用户点击确定')
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      } else if (that.data.right_num == that.data.dimen.length) {

        var members_temp = ""
        var dimen = that.data.dimen
        var name = that.data.name
        for (var i = 0; i < dimen.length; i++) {
          members_temp += dimen[i] + ":" + name[i]

          if (i != dimen.length - 1) {
            members_temp += "/"
          }
        }

        //以组为单位签到
        wx.request({
          url: getApp().globalData.my_server + "/mark/sign",
          data: {
            exam_id: that.data.exam_id,
            name: list.group_name,
            status: that.data.status,
            _id: that.data.group._id,
            members: members_temp
          },
          method: "POST",
          header: {
            "Content-Type": "application/x-www-form-urlencoded"
          },
          success: function(res) {
            console.log(res)
            if (res.data.error_no == 2) {
              wx.showModal({
                title: '哎呀～',
                content: res.data.error_msg,
                success: function(res) {
                  if (res.confirm) {
                    console.log('用户点击确定')
                  } else if (res.cancel) {
                    console.log('用户点击取消')
                  }
                }
              })
            } else if (res.data.error_no == 0) {
              that.setData({
                button_name: "已签到",
                submit_button: true,
                status: 1
              })
              wx.showModal({
                title: '提示～',
                content: "签到成功",
                success: function(res) {
                  if (res.confirm) {
                    console.log('用户点击确定')
                    wx.navigateBack({
                      delta: 1
                    })
                  } else if (res.cancel) {
                    console.log('用户点击取消')
                    wx.navigateBack({
                      delta: 1
                    })
                  }
                }
              })

            }
          },
          fail: function(res) {
            wx.showModal({
              title: '哎呀～',
              content: '签到失败',
              success: function(res) {
                if (res.confirm) {
                  console.log('用户点击确定')
                } else if (res.cancel) {
                  console.log('用户点击取消')
                }
              }
            })
            console.log("fail!", res)
          },
          complete: function(res) {

          }
        })

      }
    }

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this
    this.setData({
      exam_id: options.exam_id
    })
    //console.log(getApp().globalData.user.studentNo + ":" + getApp().globalData.user.name)
    //获得一次签到的详情
    wx.request({
      url: getApp().globalData.my_server + "/mark/get_sign_detail",
      data: {
        exam_id: that.data.exam_id,
        studentNo: getApp().globalData.user.studentNo + ":" + getApp().globalData.user.name
      },
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function(res) {
        console.log(res)
        if (res.data.error_no == 2) {
          wx.showModal({
            title: '哎呀～',
            content: res.data.error_msg,
            success: function(res) {
              if (res.confirm) {
                console.log('用户点击确定')
              } else if (res.cancel) {
                console.log('用户点击取消')
              }
            }
          })
        } else if (res.data.error_no == 1) {
          that.setData({
            status: 0
          })
        } else if (res.data.error_no == 0) {
          that.setData({
            group: res.data.group,
            status: 1,
            flag: 1,
            button_name: "已签到",
            submit_button: true,
            dimen: res.data.group.members.split("/").map((person) => {
              return person.split(":")[0]
            }),
            name: res.data.group.members.split("/").map((person) => {
              return person.split(":")[1]
            })
          })
        }
      },
      fail: function(res) {
        wx.showModal({
          title: '哎呀～',
          content: '获得一次签到的详情失败',
          success: function(res) {
            if (res.confirm) {
              console.log('用户点击确定')
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
        console.log("fail!", res)
      },
      complete: function(res) {

      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})